<?php

namespace app\adminmall\controller;
use app\web\model\NewModel;
use app\web\model\NewTypeModel;
use app\common\controller\AdminBaseController;
use think\Db;
/**
 * 文章分享
 */
class NewsController extends AdminBaseController
{
 	 protected $model;
	
    protected function _initialize() {
        parent::_initialize();
        $this->model = new NewModel();
		$this->type_model = new NewTypeModel();
		$this->assign('newtype_list',$this->type_model->select());
    }
    /**
     * 文章分享列表
     * @author [longx] 
     */
    public function index( $page = 1){     
        $field = 'n.*,m.store_name';
		$where = [];
		if(request()->isPost()){
	    	$param = input('post.');
			//标题
	    	if($param['title']){
	    	  $where['n.title'] = ['like', '%'.$param['title'].'%'];
	    	}
			//商家名称
	    	if($param['store_name']){
	    	  $where['m.store_name'] = ['like', '%'.$param['store_name'].'%'];
	    	}
			//时间
	    	if($param['start_time'] && $param['end_time']){
	    	  $where['n.addtime'] = ['between time', [$param['start_time'],$param['end_time']]];
	    	}
		}
        $list = Db::name('action_news')
        ->alias('n')
        ->join('mall_agent m','m.id = n.sid','LEFT')
        ->field($field)
		->where($where)
        ->paginate(10);
        return $this->fetch('index', ['list' => $list]);
    }

/**
     * 文章管理列表
     * @author [longx] 
     */
    public function indextype( $page = 1){
    	$p = [];
		if(request()->isPost()){
	    	$param = input('post.');
	    	if($param['title']){
	    	  $p['n.nametype'] = ['like', '%'.$param['title'].'%'];
	    	}
	    	if($param['store_name']){
	    	  $p['m.store_name'] = ['like', '%'.$param['store_name'].'%'];
	    	}
	    	if($param['start_time'] && $param['end_time']){
	    	  $p['n.addtime'] = ['between time', [$param['start_time'],$param['end_time']]];
	    	}
		}
    	 $list = Db::name('action_newtype')
        ->alias('n')
        ->join('mall_agent m','m.id = n.sid','LEFT')
        ->where($p)
        ->paginate(10);
    	return $this->fetch('indextype',["list" =>$list ]);
    }
    
    /**
     * 
     *添加
     * @author [longx] 
     */
    public function add()
    {
        if(request()->isAjax()){
        	$param=input('post.');
			$param['add_time'] = now_time();
			if ($this->model->allowField(true)->save($param) !== false) {
                    $this->success('添加成功', url('Manage/index'));
            } else {
                $this->error('添加失败');
            }
        }
        return $this->fetch();
    }


    /**
     * [edit 编辑]
     * @return [type] [description]
     * @author [lx]
     */
    public function edit($id)
    {
        if(request()->isAjax()){
            $param = input('post.');         
         	if ($this->model->allowField(true)->save($param,$id) !== false) {
                    $this->success('修改成功', url('Manage/index'));
            } else {
                $this->error('修改失败');
            }
        }
        $id = input('param.id');
        $this->assign('ad',$this->model->find($id));
        return $this->fetch();
    }

    
 /**
     * [del删除分类]
     * @author [lx] 
     */
    public function deletetype()
    {
        $id = input('param.id');
        if (Db::name('action_newtype')->where('id',$id)->delete()) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }

    /**
     * [del删除]
     * @author [lx] 
     */
    public function delete()
    {
        $id = input('param.id');
        if ($this->model->destroy($id)) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }
}